import React, { useReducer, useEffect } from "react";

const defaultState = {
  count: 3,
};

const reducer = (state, action) => {
  switch (action.type) {
    case "add":
      return {
        ...state,
        count: state.count + 1,
      };
    default:
      return state;
  }
};

const App = () => {
  // useReducer需要传入两个参数,第一个是reducer函数，第二个是defaultState
  // 返回值是一个数组，分别是state和dispatch
  const [state, dispatch] = useReducer(reducer, defaultState);
  useEffect(() => {
    // 请求数据
  }, []);
  return (
    <>
      <h3>useReducer</h3>
      <p>{state.count}</p>
      <button onClick={() => dispatch({ type: "add" })}>+</button>
    </>
  );
};

export default App;
